package org.neuedu.service;

import org.neuedu.bean.*;
import org.neuedu.dao.UserDao;
import org.neuedu.dao.UserDaoImpl;
import org.neuedu.utils.MD5Utils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public int updateSetUpAvatar(User user) {
        return userDao.updateSetUpAvatar(user);
    }
    @Override
    public int userReg(User user) {
        int count = userDao.serchUserByRegEmail(user.getEmail());
        if (count == 0) {
            // 密码加密
            user.setPassword(MD5Utils.MD5Encode(user.getPassword(), "utf8"));
            // 添加用户
            int i = userDao.saveUserInfo(user);
            if (i != 0) {
                // 根据邮箱把uid查询出来
                User user1 = userDao.serchUidByEmail(user);
                if (user1 != null) {
                    // 添加角色
                    userDao.saveRoleUserInfo(user1);
                }
            }
            return 1;
        }
        return 0;
    }

    @Override
    public User userLogin(User user) {
        user.setPassword(MD5Utils.MD5Encode(user.getPassword(), "utf8"));
        User user1 = userDao.serchUserByEmailAndPass(user);
        if (user1 != null) {
            List<Role> roles = userDao.serchRolesByUid(user1.getId());
            user1.setRoles(roles);
        }
        return user1;
    }

    @Override
    public int userhitcard(int id) {
        // TODO Auto-generated method stub
        return userDao.qd(id);
    }

    @Override
    public boolean Hitcard(int id) {
        // TODO Auto-generated method stub
        if(userDao.userhitcard(id)==true){
            return true;
        }
        return false;
    }
    @Override
    public List<Article> userArticle(Integer id) {
        List<Article> list = userDao.userArticle(id);
        return list;
    }

    @Override
    public List<Reply> userReply(Integer uid) {
        List<Reply> replies = userDao.userReply(uid);
        return replies;
    }

    @Override
    public User userset(Integer id) {
        User user1 = userDao.serchUserset(id);
        return user1;
    }

    @Override
    public int updateset(User user) {
        int i = userDao.updateUserset(user);
        return i;
    }

    @Override
    public List<Problem> getArticleTw() {
        User user=new User();
        UserDao userdao = new UserDaoImpl();
        List<Problem> twInfo = userDao.getTwInfo();
        user.setProblemList(twInfo);
        return twInfo;
    }

    @Override
    public User usersetpass(Integer id) {
        User updatepassset = userDao.updatepassset(id);
        return updatepassset;
    }

    @Override
    public int usersetpasss(Integer id,String nowpass,String pass) {

        User user = usersetpass(id);
        String password = user.getPassword();
        if(password.equals( MD5Utils.MD5Encode(nowpass,"utf8"))){
            //插入语句
            String s = MD5Utils.MD5Encode(pass, "utf8");
            int i = userDao.updatepasssets(id, s);
            return i;
        }
        return 0;
    }
}